package connections;

import java.net.* ;
import java.io.* ;

/**
 * this class implement the bind of the server to a client
 * @author ran
 *
 */
public class AcceptSocket implements Runnable{
	ServerSocket serverSocket;

	/**
	 * the main method of the server
	 * @param args
	 * @throws IOException
	 */
	public void run(){
	    System.out.println("Server started");
	    try { 
    		serverSocket = new ServerSocket(12346);
    	}
    	//the connection to client is failed
    	catch (IOException e){ 
    		System.err.println("Cannot listen on port 12346");
    		System.exit(-1);
    	}
	    //endless loop that runs the communication between the server to the clients
	    while (true)
	    {
	    	
	    	Socket clientSocket=null;
	    	try { 
	    		clientSocket=serverSocket.accept(); 
	    		//open new thread to handle the dialog server -> client
	    		Thread t = new Thread(new AcceptConn(clientSocket));
	    		t.start();	   
	    	}
	    	catch (IOException e) { 
	    		System.err.println("Accept failed");
	    		System.exit(-1);
	    	}		
	    }
    } 
}
